namespace Hrm.EntityFramework.MigrationsPermission
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Initial : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Applications",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 50),
                        IsEnable = c.Boolean(nullable: false),
                        CreateDatetime = c.DateTime(),
                        CreateUserCode = c.String(maxLength: 50),
                        UpdateDatetime = c.DateTime(),
                        UpdateUserCode = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Resources",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 50),
                        Name = c.String(maxLength: 50),
                        Type = c.String(maxLength: 50),
                        ParentCode = c.String(maxLength: 50),
                        Order = c.Int(nullable: false),
                        Url = c.String(maxLength: 1000),
                        GlyphiconsClass = c.String(maxLength: 50),
                        Description = c.String(maxLength: 2000),
                        IsEnable = c.Boolean(nullable: false),
                        CreateDatetime = c.DateTime(),
                        CreateUserCode = c.String(maxLength: 50),
                        UpdateDatetime = c.DateTime(),
                        UpdateUserCode = c.String(maxLength: 50),
                        Application_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Applications", t => t.Application_Id)
                .Index(t => t.Application_Id);
            
            CreateTable(
                "dbo.ResourceAndPermissions",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        IsEnable = c.Boolean(nullable: false),
                        CreateDatetime = c.DateTime(),
                        CreateUserCode = c.String(maxLength: 50),
                        UpdateDatetime = c.DateTime(),
                        UpdateUserCode = c.String(maxLength: 50),
                        Permission_Id = c.Int(),
                        Resource_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Permissions", t => t.Permission_Id)
                .ForeignKey("dbo.Resources", t => t.Resource_Id)
                .Index(t => t.Permission_Id)
                .Index(t => t.Resource_Id);
            
            CreateTable(
                "dbo.Permissions",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 50),
                        Name = c.String(maxLength: 50),
                        IsEnable = c.Boolean(nullable: false),
                        CreateDatetime = c.DateTime(),
                        CreateUserCode = c.String(maxLength: 50),
                        UpdateDatetime = c.DateTime(),
                        UpdateUserCode = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Roles",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 50),
                        Name = c.String(maxLength: 50),
                        Url = c.String(maxLength: 50),
                        Description = c.String(maxLength: 50),
                        IsEnable = c.Boolean(nullable: false),
                        CreateDatetime = c.DateTime(),
                        CreateUserCode = c.String(maxLength: 50),
                        UpdateDatetime = c.DateTime(),
                        UpdateUserCode = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Users",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 50),
                        Name = c.String(maxLength: 50),
                        Password = c.String(maxLength: 50),
                        Telephone = c.String(maxLength: 50),
                        StaffId = c.Int(),
                        IsEnable = c.Boolean(nullable: false),
                        CreateDatetime = c.DateTime(),
                        CreateUserCode = c.String(maxLength: 50),
                        UpdateDatetime = c.DateTime(),
                        UpdateUserCode = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Dictionaries",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(nullable: false, maxLength: 50),
                        Name = c.String(nullable: false, maxLength: 50),
                        Type = c.String(nullable: false, maxLength: 50),
                        ParentCode = c.String(maxLength: 50),
                        Description = c.String(maxLength: 2000),
                        Order = c.Int(nullable: false),
                        IsEnable = c.Boolean(nullable: false),
                        CreateDatetime = c.DateTime(),
                        CreateUserCode = c.String(maxLength: 50),
                        UpdateDatetime = c.DateTime(),
                        UpdateUserCode = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.RoleResourceAndPermissions",
                c => new
                    {
                        Role_Id = c.Int(nullable: false),
                        ResourceAndPermission_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Role_Id, t.ResourceAndPermission_Id })
                .ForeignKey("dbo.Roles", t => t.Role_Id, cascadeDelete: true)
                .ForeignKey("dbo.ResourceAndPermissions", t => t.ResourceAndPermission_Id, cascadeDelete: true)
                .Index(t => t.Role_Id)
                .Index(t => t.ResourceAndPermission_Id);
            
            CreateTable(
                "dbo.UserRoles",
                c => new
                    {
                        User_Id = c.Int(nullable: false),
                        Role_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.User_Id, t.Role_Id })
                .ForeignKey("dbo.Users", t => t.User_Id, cascadeDelete: true)
                .ForeignKey("dbo.Roles", t => t.Role_Id, cascadeDelete: true)
                .Index(t => t.User_Id)
                .Index(t => t.Role_Id);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.UserRoles", "Role_Id", "dbo.Roles");
            DropForeignKey("dbo.UserRoles", "User_Id", "dbo.Users");
            DropForeignKey("dbo.RoleResourceAndPermissions", "ResourceAndPermission_Id", "dbo.ResourceAndPermissions");
            DropForeignKey("dbo.RoleResourceAndPermissions", "Role_Id", "dbo.Roles");
            DropForeignKey("dbo.ResourceAndPermissions", "Resource_Id", "dbo.Resources");
            DropForeignKey("dbo.ResourceAndPermissions", "Permission_Id", "dbo.Permissions");
            DropForeignKey("dbo.Resources", "Application_Id", "dbo.Applications");
            DropIndex("dbo.UserRoles", new[] { "Role_Id" });
            DropIndex("dbo.UserRoles", new[] { "User_Id" });
            DropIndex("dbo.RoleResourceAndPermissions", new[] { "ResourceAndPermission_Id" });
            DropIndex("dbo.RoleResourceAndPermissions", new[] { "Role_Id" });
            DropIndex("dbo.ResourceAndPermissions", new[] { "Resource_Id" });
            DropIndex("dbo.ResourceAndPermissions", new[] { "Permission_Id" });
            DropIndex("dbo.Resources", new[] { "Application_Id" });
            DropTable("dbo.UserRoles");
            DropTable("dbo.RoleResourceAndPermissions");
            DropTable("dbo.Dictionaries");
            DropTable("dbo.Users");
            DropTable("dbo.Roles");
            DropTable("dbo.Permissions");
            DropTable("dbo.ResourceAndPermissions");
            DropTable("dbo.Resources");
            DropTable("dbo.Applications");
        }
    }
}
